package structure;

public class MyQueue<E> {
    private static class Node<E>{
        E val;
        Node<E> prev;
        Node<E> next;

        Node(E x) {
            val = x;
        }
    }

    private Node<E> head;
    private Node<E> last;
    private int size;

    // 1.入队列
    // 尾插
    public void offer(E val) {
        Node<E> node = new Node(val);
        if (head == last) {
            head = last = node;
            size++;
            return;
        }

        last.next = node;
        node.prev = last;
        last = node;
        size++;
    }

    // 2.出队列
    // 头删
    public E poll() {
        if (head == null) {
            return null;
        }

        E old = head.val;
        head = head.next;
        if (head == null) {
            last = null;
        }else {
            head.prev.next = null;
            head.prev = null;
        }
        size--;
        return old;
    }

    // 3.peek
    public E peek() {
        if (head == null) {
            return null;
        }else {
            return head.val;
        }
    }

}
